function request(object) {
	if (object.params !== undefined && object.params.length !== 0) {
		object.url = object.url + '?' +
			Object
			.keys(object.params)
			.map(key =>
				key + '=' +
				((object.params[key] === null || object.params[key] === undefined) ? '' :
					object.params[key] instanceof Object ? JSON.stringify(object.params[key]) :
					object.params[key])
			).join('&')
	}
	if (object.data !== undefined && object.data.length !== 0) {
		for (let key in object.data) {
			if (object.data[key] === null || object.data[key] === undefined) {
				object.data[key] = ''
			} else if (object.data[key] instanceof Object) {
				object.data[key] = JSON.stringify(object.data[key])
			}
		}
	}
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: getApp().globalData.baseUrl + object.url,
			files: object.files ? object.files : [],
			fileType: 'image',
			file: object.file ? object.file : null,
			filePath: object.filePath ? object.filePath : '',
			name: object.name ? object.name : '',
			header: {
				'Authorization': 'Bearer ' + uni.getStorageSync('token')
			},
			formData: object.data,
			success: response => {
				let res = {}
				try {
					res = JSON.parse(response.data)
				} catch (e) {}
				const status = response.statusCode

				if (status === 200 || // Ok
					status === 201 || // Created
					status === 204 // NoContent
				) {
					if (response.data.message) {
						uni.showToast({
							icon: 'success',
							title: res.message
						});
					}
				} else if (status === 401) // Unauthorized
				{
					uni.removeStorageSync('token')
					uni.removeStorageSync('userInfo')
					reject(new Error("您的身份验证已过期,请重新登录!"))
				} else if (status === 403) // Forbid
				{
					uni.showToast({
						icon: 'none',
						title: '您没有该操作权限!'
					});
					reject(new Error("您没有该操作权限!"))
				} else if (status === 404) // NotFound
				{
					uni.showToast({
						icon: 'none',
						title: res.message || '未找到内容!'
					});
					reject(new Error(res.message || '未找到内容!'))
				} else {
					reject(new Error(status + ': ' + (res.message || '请求出现异常!')))
				}
				resolve(res)
			},
			fail: (error) => {
				reject(error)
			},
			complete: () => {}
		});
	})
}

export default request